package com.ecoolex.chinadutyfree.retailer.core.manager;

import com.ecoolex.chinadutyfree.retailer.core.dao.DirectMailGoodsProductIncrementDataDAO;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsProductIncrementData;
import com.ecoolex.chinadutyfree.retailer.enums.DirectMailGoodsOrderStatus;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.DateUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DirectMailGoodsProductIncrementDataManager {
    @Autowired
    private DirectMailGoodsProductIncrementDataDAO dao;
    
    /**
	 * 统计直邮产品增量数据，定时任务每天零点执行一次
	 */
	public void productIncrementData(String updateUser) {
		// 获取当前日期前一天的日期字符串
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		Date now = new Date();
		calendar.setTime(now);
		calendar.add(Calendar.DATE, -1);
		String yesterday = format.format(calendar.getTime());
		// 获取当前日期
		Date today = new Date();
		DateUtil.cleanTimePart(today);
		dao.add(today,
			updateUser,
			now,
			updateUser,
			now,
			DirectMailGoodsProductIncrementData.NOT_DELETED,
			DirectMailGoodsOrderStatus.PAID.real(),
			DirectMailGoodsOrderStatus.AFTER_SALES.real(),
			DirectMailGoodsOrderStatus.WAIT_RECECEIVE.real(),
			DirectMailGoodsOrderStatus.COMPLETE.real(),
			yesterday);
	}

	/**
	 * 根据日期筛选产品增量数据
	 * 
	 * @param startDate
	 * @param endDate
	 * @param CustomName
	 * @param sorts
	 * @param page
	 * @return
	 */
	public Pagination<DirectMailGoodsProductIncrementData> queryListByDate(Date startDate, Date endDate, String customName, Sorting[] sorts, Page page) {
		return Pagination.create(page,
			dao.countByDate(startDate, endDate, customName),
			() -> dao.quertByDate(startDate,
				endDate,
				customName,
				Sortings.builder(sorts).toSql(),
				page));
	}
}